package com.zyl.file.controller;


import com.zyl.file.constant.LogsConstants;
import com.zyl.file.result.Result;
import com.zyl.file.service.FileDealService;
import com.zyl.file.util.AssertUtils;
import com.zyl.file.vo.DownFileVO;
import log.annotation.ApiLog;
import log.annotation.OperationLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件处理
 *
 * @author zyl
 * @since 2023/4/26 14:12
 */
@Slf4j
@RestController
@RequestMapping("storage/deal")
public class FileDealController {

    @Resource
    private FileDealService fileDealService;

    @Resource
    protected HttpServletRequest request;
    @Resource
    protected HttpServletResponse response;

//    /**
//     * 图片编辑PC
//     *
//     * @param pictureRotatingVO
//     * @return
//     */
//    @OperationLog(LogsConstants.UPDATE_IMG_PC)
//    @PostMapping("updateImgToPc")
//    Result<List<SysFilePictureRotatingDTO>> updateImgToPc(@RequestBody PictureRotatingVO pictureRotatingVO) {
//        return Result.success(fileDealService.updateImg(pictureRotatingVO, getToken()));
//    }
//
//    /**
//     * 图片编辑APP
//     *
//     * @param pictureRotatingVO
//     * @return
//     */
//    @OperationLog(LogsConstants.UPDATE_IMG_APP)
//    @PostMapping("updateImgToApp")
//    Result<List<SysFilePictureRotatingDTO>> updateImgToApp(@RequestBody PictureRotatingVO pictureRotatingVO) {
//        return Result.success(fileDealService.updateImg(pictureRotatingVO, getToken()));
//    }
//
//    /**
//     * 图片预编辑
//     *
//     * @param pictureRotatingVO
//     * @return
//     */
//    @OperationLog(LogsConstants.PRETREAT_UPDATE_IMG)
//    @PostMapping("pretreatUpdateImg")
//    Result<List<ResponseEntity<byte[]>>> pretreatUpdateImg(@RequestBody PictureRotatingVO pictureRotatingVO) {
//        List<ResponseEntity<byte[]>> inputStreamList = fileDealService.pretreatUpdateImg(pictureRotatingVO);
//        return Result.success(inputStreamList);
//    }
//
//    /**
//     * 图片旋转
//     *
//     * @param pictureRotatingVO
//     * @return
//     */
//    @OperationLog(LogsConstants.PICTURES_ROTATING)
//    @PostMapping("picturesRotating")
//    Result<List<SysFilePictureRotatingDTO>> picturesRotating(@RequestBody PictureRotatingVO pictureRotatingVO) {
//        return Result.success(fileDealService.picturesRotating(pictureRotatingVO, getToken()));
//    }

//    /**
//     * 文件合并
//     *
//     * @param ecmMergeVo
//     * @return
//     */
//    @OperationLog(LogsConstants.FILE_MERGE)
//    @PostMapping("mergeFile")
//    Result<SysFileDTO> mergeFile(@RequestBody FileMergeVO ecmMergeVo) {
//        return Result.success(fileDealService.mergeFile(ecmMergeVo, getToken()));
//    }
//
//    /**
//     * 文件拆分
//     *
//     * @param ecmSplitPdfVo
//     * @return
//     */
//    @OperationLog(LogsConstants.FILE_SPLIT)
//    @PostMapping("splitFile")
//    Result<List<SysFileDTO>> splitFile(@RequestBody FileSplitPdfVO ecmSplitPdfVo) {
//        return Result.success(fileDealService.splitFile(ecmSplitPdfVo, getToken()));
//    }

//    /**
//     * 创建输入流资源
//     *
//     * @param fileId 文件ID
//     * @param type   操作类型 0-查看  1-打印  用于后续调取这个接口判断是否加水印
//     * @return
//     */
//    @GetMapping("createInputStreamResources")
//    @OperationLog(LogsConstants.CREATE_INPUTSTREAM_RESOURCES)
//    public Result createInputStreamResources(HttpServletRequest request, HttpServletResponse response, Long fileId, Integer type) {
//        fileDealService.createInputStreamResources(request, response, fileId, getToken(), type);
//        return Result.success(null);
//    }


    /**
     * 创建输入流资源
     *
     * @param fileId
     * @return
     */
    @GetMapping("createInputStreamResourcesCache")
    @OperationLog(LogsConstants.CREATE_INPUTSTREAM_RESOURCES)
    public void createInputStreamResourcesCache(HttpServletRequest request, HttpServletResponse response, Long fileId) {
        fileDealService.createInputStreamResourcesCache(request, response, fileId);
    }

    /**
     * 文件下载(分片下载)
     *
     * @param zipName
     * @return
     */
    @PostMapping("downFile")
    @OperationLog(LogsConstants.FILE_DOWN)
    public Result downFile(@RequestParam String zipName, @RequestParam List<Long> fileId ,@RequestParam Integer isPack) {
//        AssertUtils.isNull(getToken(), "很遗憾，您没下载权限");
        String sessionId = request.getSession().getId();
        DownFileVO downFileVO = new DownFileVO();
        downFileVO.setFileId(fileId);
        downFileVO.setZipName(zipName);
        downFileVO.setIsPack(isPack);
        fileDealService.downFile(sessionId, downFileVO,response);
        return Result.success();
    }

    /**
     * 文件下载（普通）
     *
     * @param busiNo
     * @return
     */
    @PostMapping("downFileByResponseEntity")
    @OperationLog(LogsConstants.FILE_DOWN)
    public ResponseEntity<byte[]> downFileByResponseEntity(@RequestParam String busiNo, @RequestParam List<Long> fileId ,@RequestParam Integer isPack) {
//        AssertUtils.isNull(getToken(), "很遗憾，您没下载权限");
        String sessionId = request.getSession().getId();
        DownFileVO downFileVO = new DownFileVO();
        downFileVO.setFileId(fileId);
        downFileVO.setZipName(busiNo);
        downFileVO.setIsPack(isPack);
        ResponseEntity<byte[]> responseEntity = fileDealService.downFileByResponseEntity(sessionId, downFileVO, response);
        return responseEntity;
    }


    /**
     * 获取文件url
     *
     * @param id
     * @return
     */
    @PostMapping(value = "getFileUrl", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(LogsConstants.OCR)
    public Result<String> getFileUrl(@RequestParam("id") Long id) {
        return Result.success(fileDealService.getFileUrl(id));
    }

//    /**
//     * 混贴拆分
//     * @param mixedPastingSplitDTO
//     * @return
//     */
//    @PostMapping(value = "mixedPastingSplit", consumes = MediaType.APPLICATION_JSON_VALUE)
//    @OperationLog(LogsConstants.OCR)
//    Result<List<SysFileDTO>> mixedPastingSplit(@RequestBody MixedPastingSplitDTO mixedPastingSplitDTO) {
//        return Result.success(fileDealService.mixedPastingSplit(mixedPastingSplitDTO));
//    }

    /**
     * 删除文件
     *
     * @param fileId
     * @return
     */
    @PostMapping(value = "deleteFile", consumes = MediaType.APPLICATION_JSON_VALUE)
    @OperationLog(LogsConstants.FILE_DELETE)
    Result deleteFile(Long fileId) {
        fileDealService.deleteFile(fileId);
        return Result.success(null);
    }

}
